<?php

namespace app\admin\controller;

class Market extends Base
{
    protected function _infoModule()
    {
        return array(
            'info' => array(
                'name' => '坊市',
                'description' => '坊市',
            ),
            'menu' => array(
                array(
                    'name' => '装备',
                    'url' => url('equip'),
                    'icon' => 'list',
                ),
                array(
                    'name' => '道具',
                    'url' => url('prop'),
                    'icon' => 'list',
                ),
            ),
            '_info' => array(

            ),
        );
    }

    public function equip()
    {
        $request = $this->request;

        //筛选条件
        $where = [];
        $pageMaps = [];

        $id = $request->post('id');
        $uid = $request->post('uid');
        $zbid = $request->post('zbid');
        $name = $request->post('name');
        $lv = $request->post('lv');

        if ($id) {
            $where[] = ['payid','=',$id];
        }
        if ($uid) {
            $where[] = ['uid','=',$uid];
        }
        if ($zbid) {
            $where[] = ['zbid','=',$zbid];
        }
        if ($name) {
            $where[] = ['zbname','=',$name];
        }
        if ($lv) {
            $where[] = ['zblv','=',$lv];
        }

        //URL参数
        $pageMaps['id'] = $id;
        $pageMaps['uid'] = $uid;
        $pageMaps['zbid'] = $zbid;
        $pageMaps['name'] = $name;
        $pageMaps['lv'] = $lv;

        $pageAry = [
            'list_rows' => 15,
            'page' => input('page', 1),
            'path' => 'index',
            'query' => $pageMaps,
        ];

        //查询数据
        $list = model('MarketEquip')->loadList($where, $pageAry);
        if(!empty($list)){
            foreach ($list as $key=>$val){
                $val['uname'] = model('Role')->getWhereValue(['uid'=>$val['uid']], 'uname');
                $val['zbtoolName'] = model('Equip')->getZbtoolAttr($val['zbtool']);

                if($val['zblv']==0){
                    $val['zblv'] = model('Equip')->getWhereValue(['zbid'=>$val['zbid']], 'zblv');
                    model('MarketEquip')->saved(['payid'=>$val['payid'],'zblv'=>$val['zblv']]);
                }
            }
        }
        $this->assign('list', $list);
        $this->assign('_page', $list->render());
        $this->assign('pageMaps', $pageMaps);
        $this->assign('total', $list->total());

        return $this->fetch('equip');
    }

    public function prop()
    {
        $request = $this->request;

        //筛选条件
        $where = [];
        $pageMaps = [];

        $id = $request->post('id');
        $uid = $request->post('uid');
        $djid = $request->post('djid');
        $name = $request->post('name');

        if ($id) {
            $where[] = ['payid','=',$id];
        }
        if ($uid) {
            $where[] = ['uid','=',$uid];
        }
        if ($djid) {
            $where[] = ['djid','=',$djid];
        }
        if ($name) {
            $where[] = ['djname','=',$name];
        }

        //URL参数
        $pageMaps['id'] = $id;
        $pageMaps['uid'] = $uid;
        $pageMaps['djid'] = $djid;
        $pageMaps['name'] = $name;

        $pageAry = [
            'list_rows' => 15,
            'page' => input('page', 1),
            'path' => 'index',
            'query' => $pageMaps,
        ];

        //查询数据
        $list = model('MarketProp')->loadList($where, $pageAry);
        if(!empty($list)){
            foreach ($list as $key=>$val){
                $val['uname'] = model('Role')->getWhereValue(['uid'=>$val['uid']], 'uname');
            }
        }
        $this->assign('list', $list);
        $this->assign('_page', $list->render());
        $this->assign('pageMaps', $pageMaps);
        $this->assign('total', $list->total());

        return $this->fetch('prop');
    }

    /**
     * 删除
     */
    public function delEquip()
    {
        $request = $this->request;
        $id = $request->post('id');
        if (empty($id)) {
            returnJson(0, '参数不能为空');
        }

        $info = model('MarketEquip')->getInfo($id);

        if (model('MarketEquip')->del($id)) {
            model('RoleEquip')->saved(['zbnowid'=>$info['zbnowid'],'uid'=>$info['uid'],'sid'=>$info['sid']]);

            returnJson(1, '删除成功！');
        } else {
            returnJson(0, '删除失败');
        }
    }
    public function delProp()
    {
        $request = $this->request;
        $id = $request->post('id');
        if (empty($id)) {
            returnJson(0, '参数不能为空');
        }

        $info = model('MarketProp')->getInfo($id);

        if (model('MarketProp')->del($id)) {
            $whwew['uid'] = $info['uid'];
            $whwew['djid'] = $info['djid'];
            $infoRoleProp = model('RoleProp')->getWhereInfo($whwew);
            if(!empty($infoRoleProp)){
                $djsum = $infoRoleProp['djsum'] + $info['djcount'];
                model('RoleEquip')->saved(['id'=>$infoRoleProp['id'],'djsum'=>$djsum]);
            }else{
                $sid = model('Role')->getWhereValue(['uid'=>$info['uid']], 'sid');

                $add['uid'] = $info['uid'];
                $add['sid'] = $sid;
                $add['djid'] = $info['djid'];
                $add['djname'] = $info['djname'];
                $add['djzl'] = 2;
                $add['djcount'] = $info['djcount'];
                model('RoleEquip')->add($add);
            }

            returnJson(1, '删除成功！');
        } else {
            returnJson(0, '删除失败');
        }
    }

}

